package cn.jiedh.app.meiweidk.Utils.tool;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import cn.jiedh.app.meiweidk.app.EnvConfig;
import cn.jiedh.app.meiweidk.app.MyApp;

/**
 * @name：
 * @author：
 * @phone： 13308334117
 * @createTime： 2017/2/8
 * @modifyTime： 2017/2/8
 * @explain：
 */

public class UrlUtils {
    //    public static String BASE_URL = "https://jqmm.houhan.com/app/jqm-v2";  //正式地址
    //    public static String BASE_URL = "http://192.168.0.203:8765/app/mxk-v1"; //测试
//    public static String BASE_URL = "http://192.168.0.203:8765/app/mxk-v1"; //枯荣
    public static String version = DeviceUtils.getVersionCode(MyApp.mContext) + "";
    public static String os = "Android";
    public static String channel = OverallData.getChannel();
    //    public static String appKey = AuditConstant.releaseAppKey; //测试用： 0F32F4DEE3A674B4  正式环境：AD61F4DD298F6332  测试环境： mxk
    public static String uuid = DeviceUtils.getUUID(MyApp.mContext);

//    public static String COMMUNITY_URL="http://192.168.0.195:8080/community.html";//社区
    /**
     * @param BURIED_URL 埋点接口
     */
    private static final String BURIED_URL = "https://api.reported.data.houhan.com/api-v1/app/action/reported";//正式地址
//    public static final String BURIED_URL = "http://192.168.0.202:8765/api-v1/app/action/reported";//测试地址
//    private static final String BURIED_URL = "http://219.153.176.90:8766/api-v1/app/action/reported";//测试地址外网

    public static String GETAPPLICATIONINFO = "/base/unwantedLogin/application/applicationInfo";// 应用基本信息
    public static String UPDATEAPP = "/base/unwantedLogin/application/versionUpdate";// 版本更新
    public static String QUESTIONLIST = "/sms/unwantedLogin/question/questionList"; //常见问题列表
    public static String BROWSELIST = "/user/needLogin/userService/browseList";  //浏览记录列表
    public static String BUSINESS = "/sms/unwantedLogin/business/businessList";//商务合作
    public static String PRODUCTINFO = "/sms/unwantedLogin/product/productInfo";//贷款详情
    public static String PRODUCTLIST = "/sms/unwantedLogin/product/productList";//贷款列表
    public static String LOANTYPELIST = "/sms/unwantedLogin/loanType/loanTypeList";//贷款类型列表
    public static String COMMENTLIST = "/sms/unwantedLogin/comment/commentList";//贷款详情评论列表
    public static String ADDCOMMENT = "/user/needLogin/userService/addComment";//添加评论
    public static String RAIDERSLIST = "/sms/unwantedLogin/raiders/raidersList";//攻略列表
    public static String RAIDERSINFO = "/sms/unwantedLogin/raiders/raidersInfo";//攻略详情
    public static String UPDATAPASSWORD = "/user/needLogin/loginOrUpdate/updataPassword";//修改密码
    public static String IMAGECODE = "/base/unwantedLogin/code/imgCode";//图形验证码

    //    public static String LOGINSMSCODE = "/base/unwantedLogin/code/loginCode";//验证码登录（短信验证码）
    public static String LOGINSMSCODE = "/base/unwantedLogin/code/sendAuthCode";//修改版验证码登录（短信验证码）

    public static String REGISTERSMSCODE = "/base/unwantedLogin/code/registerCode";//注册（短信验证码）
    public static String FORGETSMSCODE = "/base/unwantedLogin/code/forgetCode";//忘记密码（短信验证码）
    public static String FORGETPSW = "/user/unwantedLogin/loginOrUpdate/forgetPasswordOne";//忘记密码
    public static String RESETPSW = "/user/unwantedLogin/loginOrUpdate/forgetPasswordTwo";//重置密码

    public static String VERIFICATIONLOGIN = "/base/unwantedLogin/code/loginAndReg";//短信验证码登录
    //public static String VERIFICATIONLOGIN = "/user/unwantedLogin/loginOrUpdate/mwdkLogin";//短信验证码登录(新改版)

    public static String PSWLOGIN = "/user/unwantedLogin/loginOrUpdate/passwordLogin";//密码登录
    public static String REGISTER = "/user/unwantedLogin/loginOrUpdate/register";//注册

    public static String BANNER = "/sms/unwantedLogin/banner/bannerList";//	banner列表 新增type参数 区分攻略banner和广告位
    public static String BANNERDETAIL = "/sms/unwantedLogin/banner/bannerInfo";//banner详情
    public static String ROLL_MSG = "/sms/unwantedLogin/carousel/carouselList";// 轮播信息列表
    public static String HOME_PRODUCT = "/sms/unwantedLogin/indexSearch/indexSearchList";// 首页模块
    public static String HOME_PRODUCT_SEARCH = "/sms/unwantedLogin/indexSearch/indexSearchProductList";//3.5 首页模块点击查询的产品列表
    public static String NEW_HOT = "/sms/unwantedLogin/product/productList";//产品列表查询(首页热门和新品)
    public static String MSGLIST = "/sms/needLogin/message/messageList";//	消息列表
    public static String MSGDETAIL = "/sms/needLogin/message/messageInfo";//  消息详情
    public static String MSGID = "/sms/needLogin/message/getMessageListId";//获取消息ID,用户终端自己处理是否未读
    public static String SURVEYGET = "/sms/needLogin/questionnaire/questionnaireInfo";// 问卷详情
    public static String SURVEYSEND = "/sms/needLogin/questionnaire/addQuestionnaire";// 填写问卷
    public static String CLEARALLBROWSE = "/user/needLogin/userService/emptyBrowse"; //清空浏览记录
    public static String ADDBROWSE = "/user/needLogin/userService/addBrowse";//添加用户浏览记录
    public static String ADVER = "/sms/unwantedLogin/adver/getAdver";//广告
    public static String APPLICATIONPAGE = "/base/unwantedLogin/application/applicationPage";//版面切换
    public static String SSOLOGIN = "/user/unwantedLogin/loginOrUpdate/ssoLogin"; //中国移动SSO一键登录接口
    public static String SwitchStatus = "/sms/unwantedLogin/switch/getSwitchStatus"; //type 0 新用户专享开关 1 安卓一键登录开关 2 ios一键登录开关
    public static String MONEYANDYONGTU = "/sms/unwantedLogin/NewExclusiveConfig/getLoanAmountAndUsed"; //贷款金额及用途
    public static String CheckRegisterTimeExtended = "/user/needLogin/userService/checkRegisterTimeExtended";//新人专区展示-判断注册时间
    public static String GetNewExclusiveProductList = "/sms/needLogin/newExclusiveProduct/getNewExclusiveProductList"; //新人专享数据接口 获取列表数据
    public static String MAINRM = "/sms/unwantedLogin/raiders/getCommunityTopicList"; //社区新闻分页查询接口(社区一般新闻、社区热门新闻、首页热门新闻)

    public static String GET_PERSONAL_INFO = "/base/unwantedLogin/code/findUserInfo";//获取个人资料

    public static String SUBMIT_PERSONAL_INFO = "/user/needLogin/userService/putPersonalInfo";//提交个人资料
    public static String ADD_FEEDBACK = "/sms/needLogin/feedBack/addFeedBack";//提交意见反馈
    public static String GET_FEEDBACK_LIST = "/sms/needLogin/feedBack/getFeedBackList";//意见反馈查询接口
    public static String UPLOADE_USER_IMAGE = "/user/needLogin/userService/putUserImg";//上传头像
    public static String GREEN_LAND = "/sms/unwantedLogin/greenChannel/switchs";//绿色通道
    public static String GREEN_LAND_LIMIT = "/sms/unwantedLogin/greenChannel/verifyUpperLimit";//绿色通道次数
    public static String GREEN_LAND_DATA="/sms/unwantedLogin/greenChannel/verifyPersonalData";//绿色通道信息
    public static String SAVE_USER_INFO="/user/needLogin/userService/addUserInfo";//实名认证

    public static String GREEN_LAND_PRODUCT="/sms/unwantedLogin/greenChannel/product";//绿色通道随机产品
    public static String CODE_AUTH_IF="/base/unwantedLogin/code/codeAuthIf";//检测验证码
    public static String AMOUNMATE="/user/needLogin/userService/amounMate";//我的额度

    public static String PRODUCT_LINK_TRANSFER="/sms/unwantedLogin/product/productLinkTransfer";//是否是联合登陆产品
    public static String authMateSwitchs="/sms/unwantedLogin/greenChannel/authMateSwitchs";//我的额度开关

    public static String INDEX_POP="/user/needLogin/userService/indexPop";//首页弹窗

    public static String COLLECT="/base/api/click/collect";//功能统计

    /**
     * 请求参数处理
     *
     * @param
     * @return
     */
    public static Map<String, Object> getMaps(Map<String, Object> maps, boolean isEncrypt) {
        Map<String, Object> map = new HashMap<>();
        map.put("os", os);
        map.put("version", version);
        map.put("channel", channel);
        map.put("appKey", RSAUtils.APP_KEY);
        map.put("uuid", uuid);


        if (SPUtils.getIsLogin()) {
            map.put("token", SPUtils.getToken(MyApp.mContext));
        }else {
            map.put("token", "");
        }
        String encryptStr = encrypt(maps, isEncrypt);
        L.i("加密后的参数", encryptStr);
        map.put("data", encryptStr);
        map.put("sign", sign(encryptStr));
        L.i("加签后的参数", (String) map.get("sign"));
        L.i("请求参数", map.toString());

        return map;
    }


    public static Map<String, Object> getMapsp(Map<String, Object> maps, boolean isEncrypt) {
        Map<String, Object> map = new HashMap<>();
        map.put("os", os);
        map.put("version", version);
        map.put("channel", channel);
        map.put("appKey", RSAUtils.APP_KEY);
        map.put("uuid", uuid);


        if (SPUtils.getIsLogin()) {
            map.put("token", SPUtils.getToken(MyApp.mContext));
        }

        String encryptStr = encrypt(maps, isEncrypt);
        L.i("加密后的参数", encryptStr);
        map.put("data", encryptStr);
        map.put("sign", sign(encryptStr));
        L.i("加签后的参数", (String) map.get("sign"));
        L.i("请求参数", map.toString());

        return map;
    }


    private static String sign(String json) {
        L.d("加签前的参数", json);
        try {
            return RSAUtils.sign(json.getBytes("UTF-8"), RSAUtils.PRIVATE_KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String encrypt(Map<String, Object> map, boolean isEncrypt) {
        JSONObject json = new JSONObject();
        L.d("加密前参数", map.toString());
        try {
            if (map != null) {
                for (String key : map.keySet()) {
                    if (isEncrypt) {
                        //只对手机号和密码加密
                        if ("phone".equals(key) || "password".equals(key) || "oldPassword".equals(key) || "newPassword".equals(key)) {
                            try {
                                byte[] byteData = (map.get(key).toString()).getBytes("UTF-8");

//                                if ("phone".equals(key)){
//                                    json.put(key, RSAUtils.encryptByPublicKey(EnvConfig.PHONE_PUBLIC_KEY, byteData));
//                                }else {
//                                    json.put(key, RSAUtils.encryptByPublicKey(RSAUtils.PUBLIC_KEY, byteData));
//                                }
//                                L.i("PUBLIC_KEY======"+RSAUtils.PUBLIC_KEY);


                                L.i(EnvConfig.PHONE_PUBLIC_KEY.length()+"----"+RSAUtils.PUBLIC_KEY.length());

                                String value = RSAUtils.encryptByPublicKey(RSAUtils.PUBLIC_KEY, byteData);

                                json.put(key, value);

                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            }
                        } else {
                            json.put(key, map.get(key));
                        }
                    } else {
                        json.put(key, map.get(key));
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
